<template>
  <div>
    <h3 style="text-align: center">用户流量概述</h3>
    <div id="myChart" style="width:100%;height:300px;"></div>
  </div>
</template>

<script>
// import userData from '../../../userData'
import FlowChartDataHandle from '../../DataHandle/FlowChartDataHandle.js'
export default {
  name: 'FlowChart',
  data() {
    return {
      msg: 'Welcome to FlowChart',
      myChart: null,
      flowChartData: [],
      flowChartTypeName:[]
    }
  },
  props: {
    datelist: Array
  },
  created() {
      this.getDefaultData("2019-12-1","2019-12-7")
  },
  watch: {
    datelist() {
      this.myChart.dispose()
      console.log('date发生更新')
      this.getSpecifiedDateData()
      console.log("日期发生更新了，重新渲染")
      this.chartInit()
    },
    flowChartData (){
      this.myChart.dispose()
      this.chartInit()
    }
  },
  mounted() {
    this.chartInit()
  },
  methods: {
    async getDefaultData(startDate,endDate) {
      let params = new URLSearchParams()
      params.append('startDate', startDate)
      params.append('endDate', endDate)
      let getCapPackClassifyForDateRes = await this.$http.post(
        '/zz/getCapPackClassifyForDate',
        params
      )
      if (getCapPackClassifyForDateRes.data.status !== 200) {
        return this.$message.error('请求数据失败')
      } else {
        // console.log(getCapPackClassifyForDateRes)
        this.flowChartData.totalFlow =
          getCapPackClassifyForDateRes.data.totalFlow
        this.flowChartData.accessStatisticalList =
          getCapPackClassifyForDateRes.data.accessStatisticalList
        let resDate =
        FlowChartDataHandle.tran(getCapPackClassifyForDateRes.data,FlowChartDataHandle.typeJson,
          new Date(startDate),0
        )
        this.flowChartData = resDate[0]
        this.flowChartTypeName = resDate[2]
        this.$Pubsub.publish("timeLineData",resDate[1])
        this.$Pubsub.publish("protocolChartData",[resDate[3],resDate[4]])
        console.log(resDate)
        console.log(getCapPackClassifyForDateRes)

      }
    },
    chartInit() {
      // console.log(this.userDT)
      if (this.myChart !== null) {
        this.myChart.dispose()
      }
      this.myChart = this.$echarts.init(document.getElementById('myChart'))
      // this.myChart.on('click', event => {
      //   console.log(event)
      //   this.$router.push({
      //     path: '/UserDetailsTable',
      //     query: { sideBar: event.name }
      //   })
      // })
      // console.log(this.flowChartData)
      // console.log(this.flowChartData)
      this.myChart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          data: this.flowChartTypeName
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.datelist
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: this.flowChartData
      })
    },
    async getSpecifiedDateData() {
      this.getDefaultData(this.datelist[0],this.datelist[this.datelist.length - 1])
    }
  }
}
</script>

<style scoped>
</style>
